<template>
  <div>
    <Card>
      <Collapse style="margin-bottom:15px;">
        <Panel>查询
          <div slot="content">
            <Form ref="formLkyTableStorage" :model="formLkyTableStorage" label-position="right" :label-width="80"
                  inline>
              <!--学院、专业、班级查询-->
              <FormItem label="所属学院" prop="name1">
                <Select v-model="coid" style="width:200px" @on-open-change="onOpenChangeCollege"
                        @on-change="onOpenChangeMajor"
                        clearable>
                  <Option v-for="item in collegeList" :value="item.id" :key="item.id">
                    {{ item.name }}
                  </Option>
                </Select>
              </FormItem>
              <FormItem label="所属专业" prop="name2">
                <Select v-model="mid" style="width:200px" @on-change="onOpenFindClassByMid" clearable>
                  <Option v-for="item in majorList" :value="item.id" :key="item.id">
                    {{ item.name }}
                  </Option>
                </Select>
              </FormItem>
              <FormItem label="所属班级" prop="name3">
                <Select v-model="cid" style="width:200px" clearable>
                  <Option v-for="item in classList" :value="item.id" :key="item.id">
                    {{ item.name }}
                  </Option>
                </Select>
              </FormItem>
              <FormItem label="表名" prop="name4">
                <Select v-model="dtmId" style="width:200px" @on-open-change="onOpenSelectFName">
                  <Option v-for="item in fNameList" :value="item.id" :key="item.id">
                    {{ item.file_name }}
                  </Option>
                </Select>
              </FormItem>
              <FormItem>
                <Button type="primary" @click="handleSubmit('formLkyTableStorage')">查询</Button>
                <Button @click="handleReset('formLkyTableStorage')" style="margin-left: 8px">重置</Button>
              </FormItem>
            </Form>
          </div>
        </Panel>
      </Collapse>
      <Table ref="tableLkyTableStorage" :data="tableData" :columns="columns" @on-selection-change="onSelectionChange"
             @on-sort-change="onSortChange"/>
      <div style="margin: 10px;overflow: hidden">
        <div style="float: right;">
          <Page :total="total" :current="current" :page-size="pageSize" @on-page-size-change="changePageSize"
                @on-change="changePage"
                :show-total="true" show-sizer></Page>
        </div>
      </div>

    </Card>
  </div>
</template>

<script>
  import {
    list,
    check,
    checkNo
  } from '@/api/lky/lkyFileCheck.js'
  import {
    createFormObj,
    createModelObj,
    getToken
  } from '@/libs/util'

  import Add from './add.vue'
  import Edit from './edit.vue'

  // 学院查询、专业、班级查询
  import {
    queryFName
  } from '@/api/lky/lkyDocumentTableManage'
  import {
    queryAll
  } from '@/api/lky/lkyCollege'
  import {
    queryAllM
  } from '@/api/lky/lkyMajor'
  import {
    queryFindClassByMid
  } from '@/api/lky/lkyClass'

  export default {
    name: 'LkyTableStorage',
    components: {
      Add,
      Edit
    },
    data() {
      return {
        formLkyTableStorage: {
          id: ''
        },
        columns: [{
          type: 'selection',
          width: 60,
          align: 'center'
        },
          {
            key: 'studentName',
            title: '学生',
            sortable: false
          },
          {
            key: 'fileName',
            title: '文件名',
            sortable: false
          },
          {
            key: 'className',
            title: '班级',
            sortable: false
          },
          {
            key: 'majorName',
            title: '专业',
            sortable: false
          },
          {
            key: 'collegeName',
            title: '学院',
            sortable: false
          },
          {
            key: 'updateTime',
            title: '更新日期',
            sortable: true
          },
          {
            title: '操作',
            key: 'action',
            width: 250,
            align: 'center',
            render: (h, params) => {
              return h('div', [
                h(
                  'Button', {
                    props: {
                      size: 'small'
                    },
                    style: {
                      marginRight: '5px'
                    },
                    on: {
                      click: () => {
                        // 自定义代码

                        // console.log(params.row)

                        // window.location.href = '/SoJpt/major/toHtml?fileName=' + params.row.new_file_name
						this.$router.push({
						  path: '/onlineView/onlineRoutePage',
						  query: {
						    newFileName: params.row.new_file_name
						  }
						})
                        // look(Object.assign({}, createModelObj(this.formLkyTableStorage, 'formLkyTableStorage'))).then(
                        //   res => {
                        //     if (res) {
                        //       // this.$emit('addRow', res.row)
                        //       // this.$emit('input', false)
                        //       // this.handleReset(name)
                        // 	  alert("哈哈")
                        //     }
                        //   }
                        // )
                      }
                    }
                  },
                  '预览'
                ),
                h(
                  'Button', {
                    props: {
                      type: 'primary',
                      size: 'small'
                    },
                    style: {
                      marginRight: '5px'
                    },
                    on: {
                      click: () => {
                        this.formLkyTableStorage.id = params.row.id
                        check(Object.assign({}, createModelObj(this.formLkyTableStorage, 'lkyTableStorage'))).then(
                          res => {
                            if (res) {

                            }
                          }
                        )

                        this.getLkyTableStorageList(this.current, this.pageSize)
                      }
                    }
                  },
                  '通过'
                ),
                h(
                  'Button', {
                    props: {
                      type: 'error',
                      size: 'small'
                    },
                    style: {
                      marginRight: '5px'
                    },
                    on: {
                      click: () => {
                        this.formLkyTableStorage.id = params.row.id
                        checkNo(Object.assign({}, createModelObj(this.formLkyTableStorage, 'lkyTableStorage'))).then(
                          res => {
                            if (res) {

                            }
                          }
                        )

                        this.getLkyTableStorageList(this.current, this.pageSize)
                      }
                    }
                  },
                  '打回'
                )
              ])
            }
          }
        ],
        tableData: [],
        total: 0,
        current: 1,
        pageSize: 10,
        isOpenAddPage: false,
        isOpenEditPage: false,
        editLkyTableStorage: {},
        selection: [],
        //创建容器
        coid: '',
        mid: '',
        cid: '',
        dtmId: '',
        collegeList: [],
        majorList: [],
        classList: [],
        fNameList: [],
        classInfo: []
      }
    },
    methods: {
      onOpenChangeCollege() {
        queryAll().then(res => {
          this.collegeList = res.data
        })
      },
      onOpenChangeMajor(coid) {
        this.cid = ''
        this.mid = ''
        queryAllM({
          'coid': this.coid
        }).then(res => {
          this.majorList = JSON.parse(JSON.stringify(res.data))
        })
      },
      onOpenFindClassByMid(mid) {
        queryFindClassByMid({
          'mid': this.mid
        }).then(res => {
          this.classList = JSON.parse(JSON.stringify(res.data))
        })
      },
      onOpenSelectFName () {
        // 说明 1是老师发布表，2是学生填写表，3是成绩评定表
        queryFName({ 'table_type': '2' }).then(res => {
          this.fNameList = res.data
        })
      },
      handleSubmit(name) {
        // 查询按钮事件
        this.getLkyTableStorageList(this.current, this.pageSize)
        this.$Message.success('Success!')
      },
      handleReset(name) {
        this.cid = ''
        this.mid = ''
        this.coid = ''
        // 重置按钮事件
        this.$refs[name].resetFields()
      },
      handleOpenPage(name) {
        // 打开页面事件(添加页,编辑页...)
        if (name === 'add') this.isOpenAddPage = true
        else if (name === 'edit') {
          this.isOpenEditPage = true
        }
      },
      addLkyTableStorage(row) {
        this.tableData.push(row)
      },
      updateLkyTableStorage(row) {
        this.$set(this.tableData, row._index, row)
      },
      batchDelete() {
        this.$Modal.confirm({
          title: '提示',
          content: '<p>确定删除吗?</p>',
          onOk: () => {
            if (this.selection.length > 0) {
              let ids = ''
              for (var i = 0; i < this.selection.length; i++) {
                ids += ',' + this.selection[i].id
              }
              batchDelete({
                ids: ids
              }).then(res => {
                for (var i = 0; i < this.selection.length; i++) {
                  this.tableData = this.tableData.filter(
                    item => item.id !== this.selection[i].id
                  )
                }
              })
            } else {
              this.$Message.error('未选中记录!')
            }
          },
          onCancel: () => {
          }
        })
      },
      getLkyTableStorageList(current, pageSize, paras) {
        let para = {
          pageNumber: current,
          pageSize: pageSize
        }
        if (paras) Object.assign(para, paras)
        // key-value放入传入后端
        Object.assign(para, {
          tid: this.tid,
          coid: this.coid,
          mid: this.mid,
          cid: this.cid,
          uid: getToken(),
          dtmId: this.dtmId
        })
        list(this.handleRequestParams(para)).then(res => {
          this.tableData = res.list
          this.current = parseInt(res.pageNumber)
          this.pageSize = parseInt(res.pageSize)
          this.total = parseInt(res.list.length)
        })
      },
      handleRequestParams(para) {
        Object.assign(para, createFormObj(this.formLkyTableStorage, 'lkyTableStorage'))
        return para
      },
      exportExcel() {
        this.$refs.tableLkyTableStorage.exportCsv({
          filename: `table-${new Date().valueOf()}.csv`
        })
      },
      changePage(pageNumber) {
        this.getLkyTableStorageList(pageNumber, this.pageSize)
      },
      changePageSize(pageSize) {
        this.getLkyTableStorageList(this.current, pageSize)
      },
      remove(index) {
        this.data6.splice(index, 1)
      },
      onSelectionChange(selection) {
        this.selection = selection
      },
      onSortChange(obj) {
        if (obj.order === 'normal') {
          this.getLkyTableStorageList(1, this.pageSize)
        } else {
          this.getLkyTableStorageList(1, this.pageSize, {
            field: obj.key,
            order: obj.order
          })
        }
      }
    },
    mounted() {
      this.tid = getToken()
      this.getLkyTableStorageList(this.current, this.pageSize)
    }
  }
</script>

<style>
</style>
